﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ManageTicket.Human
{
    public partial class frmPermission : frmBase
    {
        #region Contructor
        public frmPermission()
        {
            InitializeComponent();
        }
        #endregion

        #region Variable
        ManageTicket.Library.Control.ExecuteControl exeCtrl;

        //permission
        bool bAdd = false;
        bool bEdit = false;
        bool bDelete = false;
        bool bCancel = false;
        bool bSave = false;
        bool bFunction = false;
        bool bView = false;
        bool bFind = false;
        bool bExport = false;
        bool bSendmail = false;
        bool bPrint = false;

        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();
        ArrayList arrForm;
        ArrayList arrReport;
        Business.CodeType boCodeType = new Business.CodeType();
        Business.Permission boPermission = new Business.Permission();
        Business.PermissionReport boPermissionReport = new Business.PermissionReport();
        string GROUP_ID = "";
        #endregion

        #region Event
        private void frmPermission_Load(object sender, EventArgs e)
        {
            //execute control
            exeCtrl = new ManageTicket.Library.Control.ExecuteControl();
            exeCtrl.Dock = DockStyle.Fill;
            exeCtrl.lblDescription.Text = "Phân quyền";
            pnlExe.Controls.Add(exeCtrl);

            //permission
            clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_17");

            LoadStatusOnce();
            LoadStatus(true, true);

            GetGroup();
            arrForm = boCodeType.GetListCodeTypeByGroup("FORM");
            arrReport = boCodeType.GetListCodeTypeByGroup("RPT");

            lvwGroup_SelectedIndexChanged(null, null);
        }

        private void lvwGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwGroup.SelectedItems.Count > 0)
            {
                GROUP_ID = lvwGroup.SelectedItems[0].Tag.ToString();
                if (GROUP_ID != "")
                {
                    dtgvDetail.Rows.Clear();
                    ArrayList arr = boPermission.GetListPermissionByGroup(GROUP_ID);
                    foreach (DataObjects.Permission oPer in arr)
                    {
                        AddRowInGridView(oPer);
                    }

                    dtgvDetailReport.Rows.Clear();
                    arr = boPermissionReport.GetListPermissionReportByGroup(GROUP_ID);
                    foreach (DataObjects.PermissionReport oPer in arr)
                    {
                        AddRowInGridViewReport(oPer);
                    }
                }
            }
        }

        private void dtgvDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                switch (e.ColumnIndex)
                {
                    case 0:
                        if (dtgvDetail.Rows[e.RowIndex].Cells["colSelect"].Value.ToString().ToLower() == "true")
                        {
                            dtgvDetail.Rows[e.RowIndex].Cells["colAdd"].Value = "true";
                            dtgvDetail.Rows[e.RowIndex].Cells["colEdit"].Value = "true";
                            dtgvDetail.Rows[e.RowIndex].Cells["colDelete"].Value = "true";
                            dtgvDetail.Rows[e.RowIndex].Cells["colCancel"].Value = "true";
                            dtgvDetail.Rows[e.RowIndex].Cells["colSave"].Value = "true";
                            dtgvDetail.Rows[e.RowIndex].Cells["colView"].Value = "true";
                            dtgvDetail.Rows[e.RowIndex].Cells["colMore"].Value = "true";
                            dtgvDetail.Rows[e.RowIndex].Cells["colFind"].Value = "true";
                            dtgvDetail.Rows[e.RowIndex].Cells["colExport"].Value = "true";
                            dtgvDetail.Rows[e.RowIndex].Cells["colSendmail"].Value = "true";
                            dtgvDetail.Rows[e.RowIndex].Cells["colPrint"].Value = "true";
                        }
                        else
                        {
                            dtgvDetail.Rows[e.RowIndex].Cells["colAdd"].Value = "false";
                            dtgvDetail.Rows[e.RowIndex].Cells["colEdit"].Value = "false";
                            dtgvDetail.Rows[e.RowIndex].Cells["colDelete"].Value = "false";
                            dtgvDetail.Rows[e.RowIndex].Cells["colCancel"].Value = "false";
                            dtgvDetail.Rows[e.RowIndex].Cells["colSave"].Value = "false";
                            dtgvDetail.Rows[e.RowIndex].Cells["colView"].Value = "false";
                            dtgvDetail.Rows[e.RowIndex].Cells["colMore"].Value = "false";
                            dtgvDetail.Rows[e.RowIndex].Cells["colFind"].Value = "false";
                            dtgvDetail.Rows[e.RowIndex].Cells["colExport"].Value = "false";
                            dtgvDetail.Rows[e.RowIndex].Cells["colSendmail"].Value = "false";
                            dtgvDetail.Rows[e.RowIndex].Cells["colPrint"].Value = "false";
                        }
                        break;
                }
            }
        }

        #endregion

        #region Execute
        public override void Add()
        {
            if (chkReport.Checked)
                AddRowInGridViewReport(null);
            else
                AddRowInGridView(null);
        }

        public override void Save()
        {
            if (!CheckValid())
                return;

            //delete all
            boPermission.DeleteByGroupId(GROUP_ID);

            int result = 0;
            for (int i = 0; i < dtgvDetail.Rows.Count; i++)
            {
                if (dtgvDetail.Rows[i].Cells["colScreen"].Value.ToString().ToLower() != "")
                {
                    try
                    {
                        
                        DataObjects.Permission oPer = new DataObjects.Permission();
                        oPer.GroupId = GROUP_ID;
                        oPer.FormName = dtgvDetail.Rows[i].Cells["colScreen"].Value.ToString();
                        oPer.FuncAdd = bool.Parse(dtgvDetail.Rows[i].Cells["colAdd"].Value.ToString());
                        oPer.FuncEdit = bool.Parse(dtgvDetail.Rows[i].Cells["colEdit"].Value.ToString());
                        oPer.FuncDelete = bool.Parse(dtgvDetail.Rows[i].Cells["colDelete"].Value.ToString());
                        oPer.FuncCancel = bool.Parse(dtgvDetail.Rows[i].Cells["colCancel"].Value.ToString());
                        oPer.FuncSave = bool.Parse(dtgvDetail.Rows[i].Cells["colSave"].Value.ToString());
                        oPer.FuncView = bool.Parse(dtgvDetail.Rows[i].Cells["colView"].Value.ToString());
                        oPer.FuncMore = bool.Parse(dtgvDetail.Rows[i].Cells["colMore"].Value.ToString());
                        oPer.FuncFind = bool.Parse(dtgvDetail.Rows[i].Cells["colFind"].Value.ToString());
                        oPer.FuncExport = bool.Parse(dtgvDetail.Rows[i].Cells["colExport"].Value.ToString());
                        oPer.FuncSendmail = bool.Parse(dtgvDetail.Rows[i].Cells["colSendmail"].Value.ToString());
                        oPer.FuncPrint = bool.Parse(dtgvDetail.Rows[i].Cells["colPrint"].Value.ToString());

                        //insert
                        boPermission.Insert(oPer);
                        
                        result++;
                    }
                    catch (Exception ex)
                    {

                        //throw;
                    }
                }
                
            }

            //delete all permissionreport
            boPermissionReport.DeleteByGroupId(GROUP_ID);

            for (int i = 0; i < dtgvDetailReport.Rows.Count; i++)
            {
                if (dtgvDetailReport.Rows[i].Cells["colReport"].Value.ToString().ToLower() != "")
                {
                    try
                    {

                        DataObjects.PermissionReport oPer = new DataObjects.PermissionReport();
                        oPer.GroupId = GROUP_ID;
                        oPer.FormName = dtgvDetailReport.Rows[i].Cells["colReport"].Value.ToString();
                        oPer.FuncAllow = bool.Parse(dtgvDetailReport.Rows[i].Cells["colAllow"].Value.ToString());
                        

                        //insert
                        boPermissionReport.Insert(oPer);

                    }
                    catch (Exception ex)
                    {

                        //throw;
                    }
                }

            }

            if (result > 0)
                MessageBox.Show("Cập nhật thành công " + result.ToString() + " dòng.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("Không có dòng nào được cập nhật.", "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Information);

            lvwGroup_SelectedIndexChanged(null, null);
        }
        #endregion

        #region Method
        private void LoadStatusOnce()
        {
            exeCtrl.btnEdit.Enabled = false;
            exeCtrl.btnDelete.Enabled = false;
            exeCtrl.btnCancel.Enabled = false;
            exeCtrl.btnFunction.Enabled = false;
            exeCtrl.btnView.Enabled = false;
            exeCtrl.btnExport.Enabled = false;
            exeCtrl.btnSendmail.Enabled = false;
            exeCtrl.btnPrint.Enabled = false;
            exeCtrl.btnFind.Enabled = false;

            exeCtrl.btnEdit.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnDelete.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnCancel.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFunction.BackColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnView.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnExport.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnSendmail.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnPrint.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFind.ButtonColor = Color.FromArgb(171, 171, 171);

            exeCtrl.btnEdit.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnDelete.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnCancel.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFunction.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnView.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnExport.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnSendmail.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnPrint.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFind.ForeColor = Color.FromArgb(241, 244, 251);
        }

        private void LoadStatus(bool blAdd, bool blSave)
        {
            exeCtrl.btnSave.Enabled = blSave & bSave;
            exeCtrl.btnAdd.Enabled = blAdd & bAdd;

            exeCtrl.btnSave.ButtonColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnAdd.ButtonColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);

            exeCtrl.btnSave.ForeColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnAdd.ForeColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
        }

        private void GetGroup()
        {
            try
            {
                Business.UsersGroup boGroup = new Business.UsersGroup();
                ArrayList arr = boGroup.List();
                ListViewItem item;
                lvwGroup.Items.Clear();
                foreach (DataObjects.UsersGroup oUG in arr)
                {
                    item = new ListViewItem();
                    item.Text = oUG.GroupName;
                    item.Tag = oUG.GroupId;
                    lvwGroup.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void AddRowInGridView(DataObjects.Permission oPer)
        {
            try
            {
                System.Windows.Forms.DataGridViewRow dgvRow = new System.Windows.Forms.DataGridViewRow();
                System.Windows.Forms.DataGridViewCell dgvCell;

                //check
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? true : false;
                dgvRow.Cells.Add(dgvCell);

                //man hinh
                DataGridViewComboBoxCell dgvCellType = new DataGridViewComboBoxCell();
                dgvCellType.DataSource = arrForm;
                dgvCellType.DisplayMember = "CodeTypeName";
                dgvCellType.ValueMember = "CodeTypeId";
                dgvCellType.Value = oPer != null ? oPer.FormName : "";
                dgvRow.Cells.Add(dgvCellType);

                //add
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncAdd : false;
                dgvRow.Cells.Add(dgvCell);

                //edit
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncEdit : false;
                dgvRow.Cells.Add(dgvCell);

                //delete
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncDelete : false;
                dgvRow.Cells.Add(dgvCell);

                //cancel
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncCancel : false;
                dgvRow.Cells.Add(dgvCell);

                //save
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncSave : false;
                dgvRow.Cells.Add(dgvCell);

                //view
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncView : false;
                dgvRow.Cells.Add(dgvCell);

                //more
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncMore : false;
                dgvRow.Cells.Add(dgvCell);

                //find
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncFind : false;
                dgvRow.Cells.Add(dgvCell);

                //export
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncExport : false;
                dgvRow.Cells.Add(dgvCell);

                //send mail
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncSendmail : false;
                dgvRow.Cells.Add(dgvCell);

                //print
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncPrint : false;
                dgvRow.Cells.Add(dgvCell);

                //add row to gridview
                dtgvDetail.Rows.Add(dgvRow);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void AddRowInGridViewReport(DataObjects.PermissionReport oPer)
        {
            try
            {
                System.Windows.Forms.DataGridViewRow dgvRow = new System.Windows.Forms.DataGridViewRow();
                System.Windows.Forms.DataGridViewCell dgvCell;


                //report
                DataGridViewComboBoxCell dgvCellType = new DataGridViewComboBoxCell();
                dgvCellType.DataSource = arrReport;
                dgvCellType.DisplayMember = "CodeTypeName";
                dgvCellType.ValueMember = "CodeTypeId";
                dgvCellType.Value = oPer != null ? oPer.FormName : "";
                dgvRow.Cells.Add(dgvCellType);

                //add
                dgvCell = new DataGridViewCheckBoxCell();
                dgvCell.Value = oPer != null ? oPer.FuncAllow : false;
                dgvRow.Cells.Add(dgvCell);
                

                //add row to gridview
                dtgvDetailReport.Rows.Add(dgvRow);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private bool CheckValid()
        {
            if (GROUP_ID == "")
                return false;
            
            if (dtgvDetail.RowCount > 0)
            {
                for (int i = 0; i < dtgvDetail.Rows.Count - 1; i++)
                {
                    for (int j = i + 1; j < dtgvDetail.Rows.Count; j++)
                    {
                        if (dtgvDetail.Rows[i].Cells["colScreen"].Value.ToString() == dtgvDetail.Rows[j].Cells["colScreen"].Value.ToString())
                        {
                            MessageBox.Show("Dòng " + (i+1).ToString() + " và dòng " + (j+1).ToString() + " có giá trị Màn hình trùng nhau.", "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return false;
                        }
                    }
                }
            }

            if (dtgvDetailReport.RowCount > 0)
            {
                for (int i = 0; i < dtgvDetailReport.Rows.Count - 1; i++)
                {
                    for (int j = i + 1; j < dtgvDetailReport.Rows.Count; j++)
                    {
                        if (dtgvDetailReport.Rows[i].Cells["colReport"].Value.ToString() == dtgvDetailReport.Rows[j].Cells["colReport"].Value.ToString())
                        {
                            MessageBox.Show("Dòng " + (i + 1).ToString() + " và dòng " + (j + 1).ToString() + " có giá trị Báo cáo trùng nhau.", "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        #endregion
      
    }
}
